package com.leruan.aasimp.dao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Expression;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.leruan.aasimp.dao.CarDemioInfoDao;
import com.leruan.aasimp.entity.CarDemioInfo;

public class CarDemioInfoDaoImpl extends HibernateDaoSupport implements CarDemioInfoDao{

	public List<CarDemioInfo> findDemioList(Map<String, String> map)
			throws Exception {
		Session session = this.getSession();
		Criteria cri = session.createCriteria(CarDemioInfo.class);
		if(map.get("brand")!=null){
			Criterion  c = Expression.eq("brand", map.get("brand"));
			cri.add(c);
		}
		if(map.get("department")!=null){
			Criterion  c = Expression.eq("department", map.get("department"));
			cri.add(c);
		}
		return cri.list();
	}

	public CarDemioInfo getCarDemioInfo(String demioType) throws Exception {
		
		Session session = this.getSession();
		String hql = "from CarDemioInfo where demioType = ?";
		return (CarDemioInfo) session.createQuery(hql).setString(0, demioType).uniqueResult();
	}

	public List<CarDemioInfo> getCarDemioInfoList(String demioTypes)
			throws Exception {
		Session session = this.getSession();
		char[] cs = demioTypes.toCharArray();
		StringBuffer sb = new StringBuffer("from CarDemioInfo where demioType = ?");
		for(int i = 1; i < cs.length; i++)
			sb.append("or demioType = ?");
		Query query = session.createQuery(sb.toString());
		for(int i = 0; i < cs.length; i++)
			query = query.setCharacter(i, cs[i]);
		return query.list();
	}

}
